package com.example.h3x.androidmalware.View.Activities;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.h3x.androidmalware.R;
import com.ms.square.android.expandabletextview.ExpandableTextView;

public class ExportActivity extends AppCompatActivity {

    private static String thistext;
    private static String thisinfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_export);
        Toolbar toolbar =  findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        final ExpandableTextView exp =  findViewById(R.id.item)
                .findViewById(R.id.expand_text_view);
        final EditText email = findViewById(R.id.editText2);
        final EditText info = findViewById(R.id.editText);
        exp.setText(thistext); info.setText(thisinfo);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!email.getText().toString().isEmpty()) {
                    Send(email.getText().toString(),thisinfo+"\n"+thistext);
                    Snackbar.make(view, "Send", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                } else {
                    email.setError("This field can not be blank");
                    Snackbar.make(view, "Please write email!", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            }
        });


    }
    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }

    public static void setText(String text) {
        thistext = text;
    }
    public static void setInfo(String info) {
        thisinfo = info;
    }

    private void Send(String email, String text){
        Log.i("Send email", "");
        //sending sms string to email or messanger
        String[] TO = {String.valueOf(email)};
        String[] CC = {"xyz@gmail.com"};
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setData(Uri.parse("mailto:"));
        emailIntent.setType("text/plain");


        emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
        emailIntent.putExtra(Intent.EXTRA_CC, CC);
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
        emailIntent.putExtra(Intent.EXTRA_TEXT, text);

        try {
            startActivity(Intent.createChooser(emailIntent, "Send mail..."));
            Log.i("Finished sending email", "");
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(this,
                    "There is no email client installed.", Toast.LENGTH_SHORT).show();
        }
    }
}
